package com.woniuxy.java106crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.java106crm.entity.LoginInfo;
import com.woniuxy.java106crm.mapper.LoginInfoMapper;
import com.woniuxy.java106crm.request.ResPage;
import com.woniuxy.java106crm.service.LoginInfoService;
import com.woniuxy.java106crm.vo.FindLoginInfoVo;
import com.woniuxy.java106crm.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class LoginInfoServiceImpl implements LoginInfoService {
    @Resource
    LoginInfoMapper loginInfoMapper;
    @Override
    public Boolean addLoginInfo(LoginInfo loginInfo) {
        return loginInfoMapper.addLoginInfo(loginInfo);
    }

    @Override
    public ResPage<List<LoginInfo>> findLoginInfoByUname(FindLoginInfoVo findLoginInfoVo) {
        //1.实现分页逻辑
        PageHelper.startPage(findLoginInfoVo.getPage(), findLoginInfoVo.getSize());
        List<LoginInfo> info = loginInfoMapper.findLoginInfoByUname(findLoginInfoVo.getUserName());
        //2.获取分页的信息
        PageInfo<LoginInfo> pf = new PageInfo<>(info);
        return new ResPage<>(pf.getPageNum(),pf.getSize(),pf.getTotal(),pf.getPages(),pf.getList());

    }

    //删除登录信息
    @Override
    public Boolean delLoginInfo(Integer id) {
        return loginInfoMapper.delLoginInfo(id);
    }

    //批量删除
    @Override
    public Boolean putchDel(String ids) {
        Boolean flag = false;
        //拆分字符串
        String[] idsData = ids.split(",");
        //遍历数组
        for(String id : idsData){
            //调用dao层方法：根据id 删除信息
            flag = loginInfoMapper.delLoginInfo(Integer.valueOf(id));
        }
        return flag;
    }

    //根据登录用户查询用户信息
    @Override
    public UserVo queryUserInfoByUname(Integer uid) {
        return loginInfoMapper.queryUserInfoByUname(uid);
    }

    @Override
    public Boolean updateImage(UserVo userVo) {
        return loginInfoMapper.updateImage(userVo);
    }

    @Override
    public Boolean editUserInfo(UserVo userVo) {
        //对生日信息进行处理
        // 截取前十位
        String birthday = userVo.getBirthday().substring(0, 10);
        userVo.setBirthday(birthday);
        log.debug(userVo.toString());
        return loginInfoMapper.editUserInfo(userVo);
//        return false;
    }
}
